package cn.iocoder.yudao.module.acm.controller.admin.onboardsupply;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.acm.controller.admin.onboardsupply.vo.OnboardSupplyPageReqVO;
import cn.iocoder.yudao.module.acm.controller.admin.onboardsupply.vo.OnboardSupplyRespVO;
import cn.iocoder.yudao.module.acm.controller.admin.onboardsupply.vo.OnboardSupplySaveReqVO;
import cn.iocoder.yudao.module.acm.dal.dataobject.onboardsupply.OnboardSupplyDO;
import cn.iocoder.yudao.module.acm.service.onboardsupply.OnboardSupplyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.pojo.PageParam.PAGE_SIZE_NONE;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - 航机管理-机供品分类管理")
@RestController
@RequestMapping("/pplan/onboard-supply")
@Validated
public class OnboardSupplyController {

    @Resource
    private OnboardSupplyService onboardSupplyService;

    @PostMapping("/create")
    @Operation(summary = "创建航机管理-机供品分类管理")
    @PreAuthorize("@ss.hasPermission('acm:onboard-supply:create')")
    public CommonResult<Long> createOnboardSupply(@Valid @RequestBody OnboardSupplySaveReqVO createReqVO) {
        return success(onboardSupplyService.createOnboardSupply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新航机管理-机供品分类管理")
    @PreAuthorize("@ss.hasPermission('acm:onboard-supply:update')")
    public CommonResult<Boolean> updateOnboardSupply(@Valid @RequestBody OnboardSupplySaveReqVO updateReqVO) {
        onboardSupplyService.updateOnboardSupply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除航机管理-机供品分类管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('acm:onboard-supply:delete')")
    public CommonResult<Boolean> deleteOnboardSupply(@RequestParam("id") Long id) {
        onboardSupplyService.deleteOnboardSupply(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除航机管理-机供品分类管理")
                @PreAuthorize("@ss.hasPermission('acm:onboard-supply:delete')")
    public CommonResult<Boolean> deleteOnboardSupplyList(@RequestParam("ids") List<Long> ids) {
        onboardSupplyService.deleteOnboardSupplyListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得航机管理-机供品分类管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('acm:onboard-supply:query')")
    public CommonResult<OnboardSupplyRespVO> getOnboardSupply(@RequestParam("id") Long id) {
        OnboardSupplyDO onboardSupply = onboardSupplyService.getOnboardSupply(id);
        return success(BeanUtils.toBean(onboardSupply, OnboardSupplyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得航机管理-机供品分类管理分页")
    @PreAuthorize("@ss.hasPermission('acm:onboard-supply:query')")
    public CommonResult<PageResult<OnboardSupplyRespVO>> getOnboardSupplyPage(@Valid OnboardSupplyPageReqVO pageReqVO) {
        PageResult<OnboardSupplyRespVO> pageResult = onboardSupplyService.getOnboardSupplyPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得机供品精简列表", description = "category:1机供品2纺织品3餐具")
    public CommonResult<List<OnboardSupplyRespVO>> getSupplySimpleList(@RequestParam("category") String category, @RequestParam("name") String name) {
        OnboardSupplyPageReqVO reqVO = new OnboardSupplyPageReqVO();
        reqVO.setCategoryType(category);
        reqVO.setName(name);
        reqVO.setPageSize(PAGE_SIZE_NONE); // 不分页
        List<OnboardSupplyRespVO> list = onboardSupplyService.getOnboardSupplyPage(reqVO).getList();
        // 拼接 VO 返回，只返回 id、name、规格
        return success(convertList(list, supplyRespVO -> new OnboardSupplyRespVO()
                .setId(supplyRespVO.getId()).setName(supplyRespVO.getName()).setStandardName(supplyRespVO.getStandardName())));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出航机管理-机供品分类管理 Excel")
    @PreAuthorize("@ss.hasPermission('acm:onboard-supply:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOnboardSupplyExcel(@Valid OnboardSupplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OnboardSupplyRespVO> list = onboardSupplyService.getOnboardSupplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "航机管理-机供品分类管理.xls", "数据", OnboardSupplyRespVO.class,
                list);
    }

}